package com.eorchis.module.deptassignedinfo.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.eorchis.core.basedao.dao.IDaoSupport;
import com.eorchis.core.basedao.entity.IBaseEntity;
import com.eorchis.core.service.impl.AbstractBaseService;

import com.eorchis.log.LogRecord;
import com.eorchis.log.LogRecordParam;
import com.eorchis.log.LogThread;
import com.eorchis.module.deptassignedinfo.dao.IDeptAssignedInfoDao;
import com.eorchis.module.deptassignedinfo.domain.DepAssigneInfoQueryBean;
import com.eorchis.module.deptassignedinfo.domain.DeptAssignedInfo;
import com.eorchis.module.deptassignedinfo.domain.DeptAssignedInfoBean;
import com.eorchis.module.deptassignedinfo.service.IDeptAssignedInfoService;
import com.eorchis.module.deptassignedinfo.ui.commond.DeptAssignedInfoQueryCommond;
import com.eorchis.module.deptassignedinfo.ui.commond.DeptAssignedInfoValidCommond;
import com.eorchis.util.UniteConstants;
import com.eorchis.utils.utils.PropertyUtil;

/**
 * Title: DeptAssignedInfoServiceImpl<br>
 * Description: 单位年度参学名额表管理业务接口实现<br>
 * Company: GoldGov<br>
 * Copyright @ 2012 GoldGov .All rights reserved.<br>
 * @author Administrator
 * @createDate 2014-12-29
 * @version $Revision$
 */
// 声明本类为Server实现类，声明bean名称
@Service(value = "com.eorchis.module.deptassignedinfo.service.impl.DeptAssignedInfoServiceImpl")
public class DeptAssignedInfoServiceImpl extends AbstractBaseService implements IDeptAssignedInfoService {

	@Resource(name = "com.eorchis.module.deptassignedinfo.dao.impl.DeptAssignedInfoDaoImpl")
	private IDeptAssignedInfoDao deptAssignedInfoDao;

	@Override
	public IDaoSupport getDaoSupport() {
		return deptAssignedInfoDao;
	}

	@Override
	@SuppressWarnings("unchecked")
	public DeptAssignedInfoValidCommond toCommond(IBaseEntity entity) {
		return new DeptAssignedInfoValidCommond((DeptAssignedInfo)entity);
	}
	/**
	 * 查询分配参学名额列表
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @date 2014-12-29
	 */
	@Override
	public List<DeptAssignedInfoBean> findDeptAssignedInfoList(
			DeptAssignedInfoQueryCommond queryCommond) throws Exception {
		return deptAssignedInfoDao.findDeptAssignedInfoList(queryCommond);
	}
	/**
	 * 修改分配参学名额列表上的限定人数
	 * @param validCommond
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @createDate 2014-12-30
	 */
	@Override
	public boolean updateDeptAssignedInfo(DeptAssignedInfoValidCommond validCommond) throws Exception {
		//查询更新前的数据 by NingSS at 20150504
		DeptAssignedInfo deptAssignedInfo=deptAssignedInfoDao.find(DeptAssignedInfo.class, validCommond.getId());
		String preDeptAssignedInfo=deptAssignedInfo.toString();
		//更新操作
		boolean result=deptAssignedInfoDao.updateDeptAssignedInfo(validCommond);
		//封装更新后的数据
		deptAssignedInfo.setRestrictPersonNum(validCommond.getRestrictPersonNum());
		String deptAssignedInfoh=deptAssignedInfo.toString();
		//添加记录日志的逻辑 add by NingSS at 20150504
		LogRecord logRecord = new LogRecord();
		LogRecordParam paramChild = new LogRecordParam();
		paramChild.setOpdataId(validCommond.getId());//操作主数据id
		paramChild.setOpbefoData(preDeptAssignedInfo);//操作前数据
		paramChild.setOpafteData(deptAssignedInfoh);//操作后数据
		paramChild.setOpmodule("工委 - 调整参学名额");//操作模块
		paramChild.setOpcontent("修改报名限额");//操作说明
		paramChild.setOptype(UniteConstants.UPDATE); //操作类型
		logRecord.bulifLogParams(paramChild);
		try {
			LogThread logThread = new LogThread(paramChild);
			Thread thread = new Thread(logThread);
			thread.start();
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}
		return result;
	}
	/**
	 * 分配参学名额:未选单位列表
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @date 2014-12-30
	 */
	@Override
	public List<DeptAssignedInfoBean> findNoSelectSignupDeptList(
			DeptAssignedInfoQueryCommond queryCommond) throws Exception {
		return deptAssignedInfoDao.findNoSelectSignupDeptList(queryCommond);
	}
	/**
	 * 删除分配参学名额
	 * @param validCommond
	 * @return
	 * @throws Exception
	 * @author NingSS
	 * @createDate 2014-12-30
	 */
	@Override
	public boolean deleteDeptAssignedInfo(DeptAssignedInfoQueryCommond queryCommond)
			throws Exception {
		boolean result=false;
		//根据部门ids查询部门中参加培训的人数，如果有人参加就不能删除，否则可删
		Long count=deptAssignedInfoDao.getCountJoinTrainingClassUser(queryCommond);
		//执行删除操作
		if(0==count.intValue()){
			try{
				deptAssignedInfoDao.delete(DeptAssignedInfo.class, queryCommond.getSearchIds());
				result=true;
			}catch(Exception e){
				e.printStackTrace();
			}
		}
		return result;
	}

	@Override
	public List<DepAssigneInfoQueryBean> findDeptAssignedInfo(
			DeptAssignedInfoQueryCommond queryCommond) throws Exception {
		DepAssigneInfoQueryBean db=new DepAssigneInfoQueryBean();
		//默认查询当前年
		if(!PropertyUtil.objectNotEmpty(queryCommond.getSearchYear())){
			Calendar a=Calendar.getInstance();  
			queryCommond.setSearchYear(a.get(Calendar.YEAR));
		}
		//根据班级类型，查询该单位，当前年的限制报名人数
		queryCommond.setSearchClassType(1);
		List<DeptAssignedInfoBean> list=deptAssignedInfoDao.findDeptAssignedInfoList(queryCommond);
		if(PropertyUtil.objectNotEmpty(list)){
			int ptCount=list.get(0).getRestrictPersonNum();
			db.setPtClassRestrictNum(ptCount+"");
		}else{
			db.setPtClassRestrictNum("0");
		}
		int ptCounted=deptAssignedInfoDao.findgfDeptAssInfo(queryCommond);
		db.setPtClassRestrictedNum(ptCounted+"");
		//查询国防班
		queryCommond.setSearchClassType(2);
		list=deptAssignedInfoDao.findDeptAssignedInfoList(queryCommond);
		if(PropertyUtil.objectNotEmpty(list)){
			int gfCount=list.get(0).getRestrictPersonNum();
			db.setGfClassRestrictNum(gfCount+"");
		}else{
			db.setGfClassRestrictNum("0");
		}
		int gfCounted=deptAssignedInfoDao.findgfDeptAssInfo(queryCommond);
		db.setGfClassRestrictedNum(gfCounted+"");
		List<DepAssigneInfoQueryBean> nlist = new ArrayList<DepAssigneInfoQueryBean>();
		nlist.add(db);
		return nlist;
	}
	
	/**
	 * 查询分配参学名额列表汇总人数
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @date 2015-01-23
	 */
	@Override
	public Integer findDeptAssignedNumInfo(
			DeptAssignedInfoQueryCommond queryCommond) throws Exception {
		return deptAssignedInfoDao.findDeptAssignedNumInfo(queryCommond);
	}
	
	/**
	 * 导出
	 * @param queryCommond
	 * @return
	 * @throws Exception
	 * @author Max
	 * @date 2015-01-27
	 */
	@Override
	public List<DeptAssignedInfoBean> ExpertDeptAssignedInfoList(
			DeptAssignedInfoQueryCommond queryCommond) throws Exception {
		return deptAssignedInfoDao.ExpertDeptAssignedInfoList(queryCommond);
	}

}
